#! /usr/bin/env python
#coding=utf-8
def detect(url):
    "检测网页的编码"
    import urllib
    urlread = lambda _url: urllib.urlopen(_url).read()
    import chardet
    return chardet.detect(urlread(url))

if __name__=="__main__":
    dangdang=r"http://search.dangdang.com/book/search_pub.php?key=%B6%C5%C0%AD%C0%AD&catalog=01&SearchFromTop=1"
    print detect(dangdang)

    import urllib2
    html=urllib2.urlopen(dangdang).read()
    print html.decode("gb18030").encode("utf8")